package pdn.sci.cs.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import pdn.sci.cs.entity.Child;
import pdn.sci.cs.entity.ChildSponsor;
import pdn.sci.cs.entity.GenericList;
import pdn.sci.cs.entity.LamaNivasa;
import pdn.sci.cs.entity.SessionKey;
import pdn.sci.cs.service.ChildService;
import pdn.sci.cs.service.ChildSponsorService;
import pdn.sci.cs.service.GenericListService;
import pdn.sci.cs.service.LamaNivasaService;
import pdn.sci.cs.util.ChildrenSummary;


@Scope(value = "prototype")
public class ChildAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Autowired private ChildService childService;
	@Autowired private ChildSponsorService childSponsorService;
	@Autowired private LamaNivasaService lamaNivasaService;
	@Autowired private GenericListService genericListService;

	private static final String GROUP = "GROUP";
	private static final String NAME = "NAME";
	private static final String FULL_NAME = "FULL_NAME";
	private static final String SEX_TYPE = "SEX_TYPE";

	private ChildrenSummary childSummary;
	private Child child;
	private List<Child> list;
	private List<LamaNivasa> lamaNivasaList;
	private List<ChildSponsor> childSponsorList;
	private List<GenericList> yesNoList;
	private List<GenericList> genderTypeList;
	private List<GenericList> raceList;
	private List<GenericList> religionList;
	private List<GenericList> intakeMethodList;
	private List<GenericList> parentAvailabilityList;
	private List<GenericList> hasDoNotHaveProcessingList;
	private List<GenericList> childCategoryList;
	private List<GenericList> childGroupList;

	public String childSummary() {
		childSummary = childService.getChildrenSummary();
		return SUCCESS;
	}

	public String list() {
		if(isSponsor()) {
			childSponsorList = childSponsorService.findBySponsorId(getSessionUser().getId());
			if(childSponsorList == null | childSponsorList.size() == 0) {
				addActionError("Still you are not a sponsor. There is child attached");
			}
			return "sponsor";
		} else {
			pager = childService.findAll(pageStart, pageSize);
		}
		targetDiv = "childResultDiv";
		setActionContext(pager);
		return SUCCESS;
	}

	public String deletedList() {
		pager = childService.findAllDeleted(pageStart, pageSize);
		targetDiv = "childResultDiv";
		setActionContext(pager);
		return SUCCESS;
	}


	public String searchForm() {
		searchPopulate();
		return SUCCESS;
	}

	public String frame() {
		return SUCCESS;
	}

	public String add() {
		addMode();
		addPopulate();
		return SUCCESS;
	}
	
	public String report() {
		addPopulate();
		return SUCCESS;
	}


	public String search() {
		final String defaultValue = "";
		String name = defaultValue, sexType = defaultValue;
		String fullName = defaultValue;
		String group = defaultValue;

		if(child != null) {
			if(child.getChildGroup() == null || child.getChildGroup().isEmpty()) {
				group = defaultValue;
			} else {
				group = child.getChildGroup();
			}

			if(child.getSexType() == null || child.getSexType().isEmpty()) {
				sexType = defaultValue;
			} else {
				sexType = child.getSexType();
			}

			if(child.getName() == null || child.getName().isEmpty()) {
				name = defaultValue;
			} else {
				name = child.getName();
			}

			if(child.getFullName() == null || child.getFullName().isEmpty()) {
				fullName = defaultValue;
			} else {
				fullName = child.getFullName();
			}

			if(name.equals(defaultValue) && sexType.equals(defaultValue) && fullName.equals(defaultValue)
					&& group.equals(defaultValue)) {
				addActionError("No suitable inputs");
				return INPUT;
			}


			searchMap.put(GROUP, group);
			searchMap.put(NAME, name);
			searchMap.put(FULL_NAME, fullName);
			searchMap.put(SEX_TYPE, sexType);

			session.put(SessionKey.SEARCH_KEYS, searchMap);

		}else {
			if(session.get(SessionKey.SEARCH_KEYS) != null) {
				searchMap = (Map<String, String>)session.get(SessionKey.SEARCH_KEYS);
				group = searchMap.get(GROUP);
				name = searchMap.get(NAME);
				fullName = searchMap.get(FULL_NAME);
				sexType = searchMap.get(SEX_TYPE);
			} else {
				addActionError("Please search again");
				return SUCCESS;
			}
		}
		targetDiv = "childResultDiv";
		pager = childService.search(group, name, fullName, sexType, pageStart, pageSize);
		setActionContext(pager);


		return SUCCESS;
	}

	public String edit() {
		editMode();
		addPopulate();
		return view();
	}

	public String save() {

		if(child != null) {
			validateChild();
			if(hasErrors()) {
				addPopulate();
				return INPUT;
			} else {
				if(operationMode == OPERATION_MODE.ADD && child.getId().isEmpty()) {
					setAddSettings(child);
					child = childService.save(child);
				} else if (operationMode == OPERATION_MODE.EDIT && !child.getId().isEmpty() ) {
					setUpdateSettings(child);
					childService.update(child);
				} else {
					addActionError("Error");
					return INPUT;
				}

			}
		} else {
			addActionError("Invalid Access");
			return INPUT;
		}
		return SUCCESS;
	}

	public String view() {

		if(id == null || id.isEmpty()) {
			addActionError("Invalid Access");
			return INPUT;
		} else {
			child = childService.findById(id);
			if(child == null) {
				addActionError("Item that your are searching could not be found");
			}
		}
		return SUCCESS;
	}

	public String delete() {
		if(this.id.isEmpty()) {
			addActionError("Could not delete the entry, id is missing");
			return INPUT;
		} else {
			try {
				childService.delete(id);
			} catch(Exception e) {
				e.printStackTrace();
			}
			return list();
		}
	}

	public String activate() {
		if(this.id.isEmpty()) {
			addActionError("Could not delete the entry, id is missing");
			return INPUT;
		} else {
			try {
				childService.activateById(id);
			} catch(Exception e) {
				e.printStackTrace();
			}
			return deletedList();
		}
	}


	private void searchPopulate() {
		genderTypeList = genericListService.findListByCategoryId("C001");
		childGroupList = genericListService.findListByCategoryId("C022");
	}

	private void addPopulate() {
		lamaNivasaList = lamaNivasaService.findAll();
		yesNoList = genericListService.findListByCategoryId("C010");
		genderTypeList = genericListService.findListByCategoryId("C001");
		raceList = genericListService.findListByCategoryId("C013");
		religionList = genericListService.findListByCategoryId("C040");
		intakeMethodList = genericListService.findListByCategoryId("C014");
		parentAvailabilityList = genericListService.findListByCategoryId("C017");
		hasDoNotHaveProcessingList = genericListService.findListByCategoryId("C015");
		childCategoryList = genericListService.findListByCategoryId("C016");
		childGroupList = genericListService.findListByCategoryId("C022");
	}

	private void validateChild() {
		if(child.getCode().isEmpty()) {
			addFieldError("child.code", "Code cannot be empty");
		}

		if(child.getFullName().isEmpty()) {
			addFieldError("child.fullName", "Full Name cannot be empty");
		}

		if(child.getLamaNivasa() == null || child.getLamaNivasa().getId() == null) {
			addFieldError("child.lamaNivasa.id", "Select the lama nivasa");
		}

		if(child.getSexType().isEmpty()) {
			addFieldError("child.sexType", "Sex type cannot be empty");
		}

	}

	public Child getChild() {
		return child;
	}

	public void setChild(Child child) {
		this.child = child;
	}

	public List<Child> getList() {
		return list;
	}

	public void setList(List<Child> list) {
		this.list = list;
	}

	public List<LamaNivasa> getLamaNivasaList() {
		return lamaNivasaList;
	}

	public void setLamaNivasaList(List<LamaNivasa> lamaNivasaList) {
		this.lamaNivasaList = lamaNivasaList;
	}

	public List<GenericList> getYesNoList() {
		return yesNoList;
	}

	public void setYesNoList(List<GenericList> yesNoList) {
		this.yesNoList = yesNoList;
	}

	public List<GenericList> getGenderTypeList() {
		return genderTypeList;
	}

	public void setGenderTypeList(List<GenericList> genderTypeList) {
		this.genderTypeList = genderTypeList;
	}

	public List<GenericList> getRaceList() {
		return raceList;
	}

	public void setRaceList(List<GenericList> raceList) {
		this.raceList = raceList;
	}

	public List<GenericList> getReligionList() {
		return religionList;
	}

	public void setReligionList(List<GenericList> religionList) {
		this.religionList = religionList;
	}

	public List<GenericList> getIntakeMethodList() {
		return intakeMethodList;
	}

	public void setIntakeMethodList(List<GenericList> intakeMethodList) {
		this.intakeMethodList = intakeMethodList;
	}

	public List<GenericList> getParentAvailabilityList() {
		return parentAvailabilityList;
	}

	public void setParentAvailabilityList(List<GenericList> parentAvailabilityList) {
		this.parentAvailabilityList = parentAvailabilityList;
	}

	public List<GenericList> getHasDoNotHaveProcessingList() {
		return hasDoNotHaveProcessingList;
	}

	public void setHasDoNotHaveProcessingList(
			List<GenericList> hasDoNotHaveProcessingList) {
		this.hasDoNotHaveProcessingList = hasDoNotHaveProcessingList;
	}

	public List<GenericList> getChildCategoryList() {
		return childCategoryList;
	}

	public void setChildCategoryList(List<GenericList> childCategoryList) {
		this.childCategoryList = childCategoryList;
	}

	public List<ChildSponsor> getChildSponsorList() {
		return childSponsorList;
	}

	public void setChildSponsorList(List<ChildSponsor> childSponsorList) {
		this.childSponsorList = childSponsorList;
	}

	public List<GenericList> getChildGroupList() {
		return childGroupList;
	}

	public void setChildGroupList(List<GenericList> childGroupList) {
		this.childGroupList = childGroupList;
	}

	public ChildrenSummary getChildSummary() {
		return childSummary;
	}

	public void setChildSummary(ChildrenSummary childSummary) {
		this.childSummary = childSummary;
	}

}
